﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Interface;

namespace PDAMain.Forms.ReadingAmmeter
{
    public partial class AmmeterReadingSel : Form
    {

        public AmmeterReadingSel()
        {
            InitializeComponent();
            //初始化设置格式
           /* TPBeginDT.Format = DateTimePickerFormat.Custom;
            TPBeginDT.CustomFormat = "yyyy-MM-dd";
            TPBeginDT.Value = System.DateTime.Now;
            
            TPEndDT.Format = DateTimePickerFormat.Custom;
            TPEndDT.CustomFormat = "yyyy-MM-dd";
            TPEndDT.Value = System.DateTime.Now;*/
        }
        private void menuSearch_Click(object sender, EventArgs e)
        {
            if (!NetWorkState.checkWifiState() && !NetWorkState.checkGprsState())
            {
                MessageBox.Show("无法连接网络");
                return;
            }
            string MeterId = txtAmterID.Text.ToString().Trim();
            string BuildingId = txtroomID.Text.ToString().Trim();
            string timeStart = TPBeginDT.Value.ToString("yyyy/MM/dd hh:mm:ss");
            string timeEnd = TPEndDT.Value.ToString("yyyy/MM/dd hh:mm:ss");
            DataSet ds = DataHelper.ReadingAmmeterDao.select(MeterId, BuildingId, timeStart, timeEnd);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                dgamterInfo.Visible = false;
                dgamterInfo.TableStyles.Clear();
                dgamterInfo.Refresh();
                MessageBox.Show("无数据");
                return;
            }
            /*try
            {
                pdaWebReference.PdaService pda = PublicMethods.getobject();
                string Ammeter = pda.queryAmmeterReadingWorkOrder(AmterID, BeginDt, EndDt, Identity.dutyPerson);

                if (Ammeter != string.Empty)
                {
                    ds = PublicMethods.convertToDataSet(Ammeter);
                }
            }
            catch
            {
                string strsql = "SELECT EM_METER_ID as meterId,READMONTH as month,PRE_DEGREE as lastDegree,READDEGREE as degree,DISTRICT_ID,"
                           + "BUILDING_ID,APARMENT_ID,READINGTIME FROM  ELECTRICITYMETER  WHERE READDEGREE=0 and EM_METER_ID like '%" + AmterID + "%'";
                ds = PublicMethods.SelectData(strsql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["READINGTIME"]) < Convert.ToDateTime(BeginDt) || Convert.ToDateTime(ds.Tables[0].Rows[index]["READINGTIME"]) > Convert.ToDateTime(EndDt))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }*/
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dgamterInfo.Visible = true;
                DataTable dtmapping = ds.Tables[0];
                dtmapping = Check_Null(dtmapping);
                dgamterInfo.DataSource = dtmapping;
                DataGridTableStyle dts = new DataGridTableStyle();
                dts.MappingName = dtmapping.TableName;


                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
                col1.HeaderText = "电表号";
                col1.MappingName = "amId";
                col1.Width = 120;
                dts.GridColumnStyles.Add(col1);

                DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
                col2.HeaderText = "抄表月份";
                col2.MappingName = "month";
                col2.Width = 64;
                dts.GridColumnStyles.Add(col2);

                DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
                col3.HeaderText = "上月读数";
                col3.MappingName = "lastDegree";
                col3.Width = 65;
                dts.GridColumnStyles.Add(col3);

                DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
                col4.HeaderText = "本月读数";
                col4.MappingName = "degree";
                col4.Width = 65;
                dts.GridColumnStyles.Add(col4);



                dgamterInfo.TableStyles.Clear();
                dgamterInfo.TableStyles.Add(dts);
                dgamterInfo.Refresh();

            }


        }

        private DataTable Check_Null(DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["lastDegree"].ToString() == "")
                {
                    dt.Rows[i]["lastDegree"] = "";
                }
            }
            return dt;
        }

       
        private void menuBack_Click(object sender, EventArgs e)
        {
            AmPlanList ARL = new AmPlanList();
            ARL.Show();
            this.Close();
        }
     }
}